home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSKFLG.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  3KB  |  176 lines

  1. /*
  2.    TSKFLG.C - CTask - Flag handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16.  
  17. /*
  18.    create_flag - initialises flag.
  19. */
  20.  
  21. flagptr far create_flag (flagptr flg
  22. #if (TSK_NAMEPAR)
  23.                         ,byteptr name
  24. #endif
  25.                         )
  26. {
  27. #if (TSK_DYNAMIC)
  28.    if (flg == NULL)
  29.       {
  30.       if ((flg = tsk_alloc (sizeof (flag))) == NULL)
  31.          return NULL;
  32.       flg->flags = F_TEMP;
  33.       }
  34.    else
  35.       flg->flags = 0;
  36. #endif
  37.  
  38.    flg->wait_set = flg->wait_clear = NULL;
  39.    flg->state = 0;
  40.  
  41. #if (TSK_NAMED)
  42.    tsk_add_name (&flg->name, name, TYP_FLAG, flg);
  43. #endif
  44.  
  45.    return flg;
  46. }
  47.  
  48. /*
  49.    delete_flag - kills all processes waiting for flag
  50. */
  51.  
  52. void far delete_flag (flagptr flg)
  53. {
  54.    CRITICAL;
  55.  
  56.    C_ENTER;
  57.  
  58.    tsk_kill_queue (&(flg->wait_set));
  59.    tsk_kill_queue (&(flg->wait_clear));
  60.    flg->state = 0;
  61.    C_LEAVE;
  62.  
  63. #if (TSK_NAMED)
  64.    tsk_del_name (&flg->name);
  65. #endif
  66.  
  67. #if (TSK_DYNAMIC)
  68.    if (flg->flags & F_TEMP)
  69.       tsk_free (flg);
  70. #endif
  71. }
  72.  
  73.  
  74. /*
  75.    wait_flag_set  - Wait until flag is != 0. If flag is != 0 on
  76.                     entry, the task continues to run.
  77. */
  78.  
  79. int far wait_flag_set (flagptr flg, dword timeout)
  80. {
  81.    CRITICAL;
  82.  
  83.    C_ENTER;
  84.    if (flg->state)
  85.       {
  86.       C_LEAVE;
  87.       return 0;
  88.       }
  89.    tsk_current->retptr = NULL;
  90.    tsk_wait (&flg->wait_set, timeout);
  91.    return (int)tsk_current->retptr;
  92. }
  93.  
  94.  
  95. /*
  96.    wait_flag_clear - Wait until flag is == 0. If flag is == 0 on
  97.                      entry, the task continues to run.
  98. */
  99.  
  100. int far wait_flag_clear (flagptr flg, dword timeout)
  101. {
  102.    CRITICAL;
  103.  
  104.    C_ENTER;
  105.    if (!flg->state)
  106.       {
  107.       C_LEAVE;
  108.       return 0;
  109.       }
  110.  
  111.    tsk_current->retptr = NULL;
  112.    tsk_wait (&flg->wait_clear, timeout);
  113.    return (int)tsk_current->retptr;
  114. }
  115.  
  116.  
  117. /*
  118.    set_flag - Set flag to 1. If there are tasks waiting for the
  119.               set state, all tasks in the queue are made eligible.
  120. */
  121.  
  122. void far set_flag (flagptr flg)
  123. {
  124.    CRITICAL;
  125.  
  126.    C_ENTER;
  127.    flg->state = 1;
  128.  
  129.    while (flg->wait_set != NULL)
  130.       flg->wait_set = tsk_runable (flg->wait_set);
  131.    C_LEAVE;
  132. }
  133.  
  134.  
  135. /*
  136.    clear_flag - Set flag to 0. If there are tasks waiting for the
  137.                 clear state, all tasks in the queue are made eligible.
  138. */
  139.  
  140. void far clear_flag (flagptr flg)
  141. {
  142.    CRITICAL;
  143.  
  144.    C_ENTER;
  145.    flg->state = 0;
  146.  
  147.    while (flg->wait_clear != NULL)
  148.       flg->wait_clear = tsk_runable (flg->wait_clear);
  149.  
  150.    C_LEAVE;
  151. }
  152.  
  153.  
  154. /*
  155.    clear_flag_wait_set - Set flag to 0, then wait for set state.
  156. */
  157.  
  158. int far clear_flag_wait_set (flagptr flg, dword timeout)
  159. {
  160.    clear_flag (flg);
  161.    return wait_flag_clear (flg, timeout);
  162. }
  163.  
  164.  
  165. /*
  166.    check_flag - return current flag state.
  167. */
  168.  
  169. int far check_flag (flagptr flg)
  170. {
  171.    return flg->state;
  172. }
  173.  
  174.  
  175.  
  176.